package com.yuexin.project.dailymenu.dishtype.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringJoiner;

import com.yuexin.common.utils.StringUtils;
import com.yuexin.project.dailymenu.dishtype.domain.DishTypeItem;
import com.yuexin.project.system.dict.domain.DictData;
import com.yuexin.project.system.dict.service.IDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.ServletUtils;
import com.yuexin.common.utils.poi.ExcelUtil;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.dailymenu.dishtype.domain.DishType;
import com.yuexin.project.dailymenu.dishtype.service.ITypeService;

/**
 * 菜品分类 信息操作处理
 * 
 * @author yuexin
 * @date 2018-10-09
 */
@Controller
@RequestMapping("/dailymenu/dishtype")
public class TypeController extends BaseController
{
    private String prefix = "dailymenu/dishtype";
	
	@Autowired
	private ITypeService typeService;
	@Autowired
	private IDictDataService dataService;
	
	@RequiresPermissions("dailymenu:dishtype:view")
	@GetMapping()
	public String type()
	{


	    return prefix + "/type";
	}
	
	/**
	 * 查询菜品分类列表
	 */
	@RequiresPermissions("dailymenu:dishtype:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DishType type)
	{
		startPage();
		type.setCompanyId(ShiroUtils.getCompanyId());
        List<DishType> list = typeService.selectTypeList(type);
		DictData data=new DictData();
		data.setDictType("dinner_type");

		List<DictData>  dictDataList= dataService.selectDictDataList(data);
		 if(list!=null && list.size()>0) {
			 list.stream().forEach(item -> {
				 String ids = item.getBelongType();
				 ArrayList<String> typeDataList=new ArrayList<>();
				 StringJoiner joiner = new StringJoiner(",");
				 if (!StringUtils.isEmpty(ids)) {
					 String idList[] = ids.split(",");
					 for (String id : idList) {
						 if (!id.equals("")) {
							 if (!typeDataList.contains(id)) {
								 typeDataList.add(id);
							 }
						 }
					 }
					 for (String id : typeDataList) {

						 if (dictDataList != null && dictDataList.size() > 0) {
							 dictDataList.stream().forEach(dataItem -> {
								 if (dataItem.getDictValue().equals(id)) {
									 joiner.add(dataItem.getDictLabel());
								 }
							 });

						 }
					 }
				 }
				 item.setBelongTypeName(joiner.toString());

			 });
		 }


		return getDataTable(list);
	}
	
	@Log(title = "菜品分类", businessType = BusinessType.EXPORT)
    @RequiresPermissions("dailymenu:dishtype:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DishType type)
    {
        List<DishType> list = typeService.selectTypeList(type);
        ExcelUtil<DishType> util = new ExcelUtil<DishType>(DishType.class);
        return util.exportExcel(list, "dishtype");
    }
	
	/**
	 * 新增菜品分类
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存菜品分类
	 */
	@RequiresPermissions("dailymenu:dishtype:add")
	@Log(title = "菜品分类", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(DishType type)
	{		
		type.setCompanyId(ShiroUtils.getCompanyId());
		return toAjax(typeService.insertType(type));
	}

	/**
	 * 修改菜品分类
	 */
	@GetMapping("/edit/{typeId}")
	public String edit(@PathVariable("typeId") Integer typeId, ModelMap mmap) {
		DishType type = typeService.selectTypeById(typeId);
		String types=type.getBelongType();
		DictData data=new DictData();
		data.setDictType("dinner_type");
		List<DictData>  dictDataList= dataService.selectDictDataList(data);
		type.setDishTypeItemList(new ArrayList<>());
		if(dictDataList!=null && dictDataList.size()>0 ) {
			dictDataList.stream().forEach( dataItem->{
				DishTypeItem item=new DishTypeItem();
				item.setTypeId(dataItem.getDictValue());
				item.setIfSelected(false);
				item.setTypeName(dataItem.getDictLabel());
				type.getDishTypeItemList().add( item);
			});

		}
		if(!StringUtils.isEmpty(types)){
			String dishTypes[]=types.split(",");
			for(String id : dishTypes) {
				type.getDishTypeItemList().stream().forEach( item->{
					if(item.getTypeId().equals(id)) {
						item.setIfSelected(true);
					}
				});
			}
		}

		mmap.addAttribute("type", type);
		return prefix + "/edit";
	}
	
	/**
	 * 修改保存菜品分类
	 */
	@RequiresPermissions("dailymenu:dishtype:edit")
	@Log(title = "菜品分类", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DishType type, String [] belongTypes ) {
		String ids = "";
		if (belongTypes != null && belongTypes.length   > 0) {
			for (String id : belongTypes) {
				ids += id + ",";
			}
		}
		if(!StringUtils.isEmpty(ids)) {
			ids=","+ids;
		}
		type.setBelongType(ids);
		return toAjax(typeService.updateType(type));
	}
	
	/**
	 * 删除菜品分类
	 */
	@RequiresPermissions("dailymenu:dishtype:remove")
	@Log(title = "菜品分类", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)	{
		return toAjax(typeService.deleteTypeByIds(ids));
	}
	
}
